<?php
/*
 * Created by generator
 *
 */

class Listadherents extends Controller {

	/**
	 * Constructeur
	 */
	function Listadherents(){
		parent::Controller();
		$this->load->model('adherent_model');
		$this->load->library('session');
		$this->load->helper('url');
	}

	/**
	 * Affichage des adherents
	 */
	public function index($orderBy = null){
		$data['adherents'] = adherent_model::getAlladherents($this->db, $orderBy);
		$this->load->view('listadherents_view', $data);
	}

	/**
	 * Ajout d'un adherent
	 */
	public function add(){
		// Insertion en base
		$model = new adherent_model();
		$model->adhidadh = $this->input->post('adhidadh'); 
		$model->adhlbnom = $this->input->post('adhlbnom'); 
		$model->adhlbprn = $this->input->post('adhlbprn'); 
		$model->adhtxadr = $this->input->post('adhtxadr'); 
		$model->adhlbtel = $this->input->post('adhlbtel'); 
		$model->adhlbmai = $this->input->post('adhlbmai'); 
		$model->adhblpho = null;
		$model->save($this->db);
		
		
		// Upload du fichier photo
		$config['upload_path'] = realpath('www/photosAdherents/');
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$config['max_size']	= '0';
		$config['max_width']  = '0';
		$config['max_height']  = '0';
		$this->load->library('upload', $config);
		$path = $config['upload_path'] . "/";
		$thumbnailsPath = $path . "thumbnails" . "/";
		
		$uploadDataImage = null;
		if($this->upload->do_upload('adhlbpho_image')){
			$uploadDataImage = $this->upload->data('adhlbpho_image');
			//$this->session->set_flashdata('message', 'photo récupérée:' . $uploadDataImage);
			
		} else {
			// pas de photo à charger
			//$this->session->set_flashdata('message', $this->upload->display_errors() );
		}
		if($uploadDataImage != null) {
			$model->adhlbpho = 'adh_' . $model->adhidadh . '_img' . $uploadDataImage['file_ext'];
			//rename($path . $uploadDataImage['file_name'], $path . $model->adhlbpho);
			// creation de la grande image
			adherent_model::createThumbnail($path, $uploadDataImage['file_name'], $path, $model->adhlbpho, 100, 150);
			// creation de la petite image
			adherent_model::createThumbnail($path, $uploadDataImage['file_name'], $thumbnailsPath, $model->adhlbpho, 20, 20);
			// suppression de l'image téléchargée
			unlink($path . $uploadDataImage['file_name']);
			$model->updatePhoto($this->db);
		}
		
		
		$this->session->set_flashdata('message', 'Nouvel adherent ajouté');
		
		// Recharge la page avec les nouvelles infos
		redirect('listadherents/index'); 
	}

	/**
	 * Suppression d'un adherent
	 * @param $adhidadh identifiant a supprimer
	 */
	function delete($adhidadh){
		adherent_model::delete($this->db, $adhidadh);

		$this->session->set_flashdata('message', 'adherent supprime');

		redirect('listadherents/index'); 
	}

}
?>
